package br.com.treinaweb.parte2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Tela1 extends Activity implements OnClickListener {

	final int REQUEST_CODE = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tela1);
		
		Button btnProxima = (Button) findViewById(R.id.buttonProxima);
		btnProxima.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_tela1, menu);
		getMenuInflater().inflate(R.menu.tela1, menu);
		return true;
	}
	
	@Override
	public void onClick(View v) {
		Intent intencao = new Intent("br.com.treinaweb.action.SUBACTIVITY");
		
		EditText txtNome = (EditText) findViewById(R.id.editTextNome);
		String nome = txtNome.getText().toString();
		
		intencao.putExtra("nome", nome);
		startActivityForResult(intencao, REQUEST_CODE);
	}
	
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
		
		//TextView result = (TextView) findViewById(R.id.textViewRetorno);
		//result.setText("ResultCode [" + resultCode + "] REQUEST_CODE [" + REQUEST_CODE + "] RequestCode: ["  + requestCode + "]");
		
		switch (requestCode) {
		case REQUEST_CODE:
			TextView result = (TextView) findViewById(R.id.textViewRetorno);
			
			if (resultCode == RESULT_OK) {
				if (data.hasExtra("texto")) {
					result.setText(data.getExtras().getString("texto"));
				}
			}
			
			break;
		
		}
	}
}
